/**
* Appcelerator Titanium Platform
* Copyright (c) 2009-2011 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
**/
// Code is stripped-down version of Tweetanium, to expose new structure paradigm

(function(){
	S.ui.createNewsView = function(){
		var view = Ti.UI.createView(S.combine($$.contentView));
		
		
		
		// create table view data object
		var data = [];
		
		var xhr = Ti.Network.createHTTPClient();
		xhr.open("GET","http://api.electiontools.org/newsFeed.php");
		xhr.onload = function()
		{
			try
			{
				var doc = this.responseXML.documentElement;
				var items = doc.getElementsByTagName("item");
				var x = 0;
				
				for (var c=0;c<items.length;c++)
				{
					var item = items.item(c);
					// Image
					var itemImg = item.getElementsByTagName("image").item(0).text;
					// Title
					var itemTitle = item.getElementsByTagName("title").item(0).text;
					// Description
					var itemDescription = item.getElementsByTagName("description").item(0).text;
					// URL
					var itemUrl = item.getElementsByTagName("url").item(0).text;
					

					if (itemUrl && itemUrl.length > 0)
					{

						// Single row
						var row = Ti.UI.createTableViewRow(S.combine($$.tableRow));
						// Title
						var labelTitle = Ti.UI.createLabel(S.combine({text:itemTitle},$$.listTitleLabel));
						//row.add(labelTitle);
						
						
						var contentView = Ti.UI.createView({layout:'horizontal'});
						// Thumbnail
						var media = Ti.UI.createImageView(S.combine({url:itemImg},$$.listImgThumbnail));
						contentView.add(media);
						// Description
						var labelDescription = Ti.UI.createLabel(S.combine({text:itemDescription},$$.listDescriptionLabel));
						contentView.add(labelDescription);
						
						
						
						
						row.add(contentView);


						// Adding the data and setting thr url
						data[x++] = row;
						row.url = item.getElementsByTagName("url").item(0).text;
					}
				}
				var tableview = Titanium.UI.createTableView({data:data});
				view.add(tableview);
				
				
				/*tableview.addEventListener('click',function(e)
				{
					var w = Ti.UI.createWindow({title:doctitle});
					var wb = Ti.UI.createWebView({url:e.row.url});
					w.add(wb);
					var b = Titanium.UI.createButton({
						title:'Close',
						style:Titanium.UI.iPhone.SystemButtonStyle.PLAIN
					});
					w.setLeftNavButton(b);
					b.addEventListener('click',function()
					{
						w.close();
					});
					w.open({modal:true});
				});*/
				
				
			}
			catch(E)
			{
				alert(E);
			}
		};
		xhr.send();
		
		
		
		return view;
	};
})();







